package com.yunkuhui.www.demo.controller;

import com.yunkuhui.www.demo.form.AssetIndex;
import com.yunkuhui.www.demo.form.Condition;
import com.yunkuhui.www.demo.form.ResponseFile;
import com.yunkuhui.www.demo.component.HttpClientComponent;
import org.springframework.core.io.Resource;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/http")
public class HttpController {

	@RequestMapping("/1")
	public String doGet() {
		Map<String, Object> map = new HashMap<>();
		String url = HttpClientComponent.spliceUrl(null, map);
		System.out.println("url: " + url);
		return HttpClientComponent.sendGet(url, String.class);
	}

	@RequestMapping("/2")
	public AssetIndex doGet1() {
		Map<String, Object> map = new HashMap<>();
		map.put("contract", "aaaaaaaaaaaaaaaaaaa");
		map.put("invoice", "1111111111111111111");
		return HttpClientComponent.sendGet("http://127.0.0.1:8101/demo/http/3", map, AssetIndex.class);
	}

	@RequestMapping("/3")
	public AssetIndex testGetParam(AssetIndex assetIndex) {
		System.out.println("接收到的请求参数：" + assetIndex);
		return assetIndex;
	}

	// 文件转发测试
	@RequestMapping("/4")
	@SuppressWarnings("all")
	public void testFileSend(MultipartFile file, HttpServletResponse response) throws URISyntaxException, IOException {
		final Resource resource = file.getResource();
		MultiValueMap<String, Object> multiPartBody = new LinkedMultiValueMap<>();
		multiPartBody.add("file", resource);
		multiPartBody.add("contract", "YOUQUE");
		multiPartBody.add("invoice", "YOUQUE");
		ResponseFile responseFile = HttpClientComponent.sendPost("http://127.0.0.1:8101/demo/http/5", multiPartBody, ResponseFile.class);
		ServletOutputStream outputStream = response.getOutputStream();
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
		// response.setHeader("Content-Disposition", "attachment;filename=图片.jpg");
		// response.setContentType("image/gif;image/jpeg;image/png");
		bufferedOutputStream.write(responseFile.getBytes());
		System.out.println("文件已下载：" + responseFile.getAssetIndex());
	}

	@RequestMapping("/5")
	public ResponseFile testGetFile(MultipartFile file, AssetIndex assetIndex, String contract, String invoice) throws IOException {
		System.out.println("接收到的文件：" + file);
		System.out.println("文件名：" + file.getName());
		System.out.println("原始文件名：" + file.getOriginalFilename());
		System.out.println("文件大小：" + file.getSize());
		System.out.println("文件资源：" + file.getResource());
		System.out.println(assetIndex);
		System.out.println(contract);
		System.out.println(invoice);

		ResponseFile responseFile = new ResponseFile();
		responseFile.setBytes(file.getBytes());
		responseFile.setAssetIndex(assetIndex);
		return responseFile;
	}

	// 以 json 类型发送 http
	@RequestMapping("/6")
	public Condition testSendBodyJson() throws URISyntaxException {
		String[] arr = {"aa", "bb", "cc", "dd", "ee"};
		List<String> list = Arrays.asList(arr);

		Map<String, AssetIndex> map = new HashMap<>();
		AssetIndex assetIndex = new AssetIndex();
		assetIndex.setInvoice("aaaaaaaaaaaa");
		assetIndex.setContract("sssssssssssss");
		map.put("aaa", assetIndex);
		map.put("bbb", assetIndex);
		map.put("ccc", assetIndex);
		map.put("ddd", assetIndex);
		map.put("eee", assetIndex);
		map.put("fff", assetIndex);

		Map<String, String> mapLike = new HashMap<>();
		mapLike.put("111", "fsfhklsehfs");
		mapLike.put("222", "bjasfbafbasjkfbjkl");
		mapLike.put("333", "dfabjkfabklasffnaf");
		mapLike.put("444", "ghaensfgbajkbl");
		mapLike.put("555", ",mamdqawsfhn");

		Condition condition = new Condition();
		condition.setPlanId("00000000000000000000000000000000");
		condition.setInVal(list);
		condition.setEqualVal(map);
		condition.setLikeVal(mapLike);

		condition = HttpClientComponent.sendBodyJson("http://127.0.0.1:8101/demo/body", condition, Condition.class);
		return condition;
	}
}
